<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib  prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib  prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="redis" uri="http://www.shopjsp.com/redisTag/functions" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${redis:getSysConfig('base_path_back')}"/>
<c:set var="uploadPath" value="${redis:getSysConfig('uploadFileVisitRoot')}"/>
<c:set var="defaultImg" value="${redis:getSysConfig('website_default_img')}"/>
<script type="text/javascript">
    /**
     * 查询已关联的计量单位
     */
    <shiro:hasPermission name="back_gotoProductTypePage_queryMeasuringUnit">
    function queryMeasuringUnitInfo(){
        var selectedRow= $("#tt").bootstrapTable('getSelections');
        if(selectedRow.length==1){
            var levelCode=selectedRow[0].levelCode;
            if(levelCode==3){
                $("#productTypeId").val(selectedRow[0].productTypeId);/**获取到productTypeId值**/
                $("#btn_queryMeasuringUnit").attr("data-target", "#queryMeasuringUnitModal");/**当前分类级别是三级分类才可打开对应的模态框**/
                $("#queryMeasuringUnitModalLabel").empty().text("已关联计量单位");
                /** 重置当前未选中的列表中的查询条件**/
                $("#qform_queryMeasuringUnit")[0].reset();
                showMeasuringUnitList();
            }else{
                $("#btn_queryMeasuringUnit").attr("data-target",""); /**没有选中记录不可打开模态框**/
                showMsg("当前分类级别不是三级分类，不可查询计量单位！");
            }
        }else{
            $("#btn_queryMeasuringUnit").attr("data-target",""); /**没有选中记录不可打开模态框**/
            showMsg("请选择一行记录！");
        }
    }
    </shiro:hasPermission>

    /**
     * 已关联计量单位列表
     */
    function showMeasuringUnitList(){
    	$("#tt_measuringUnitList").bootstrapTable('destroy'); 
        var $tt_measuringUnitList = $("#tt_measuringUnitList").bootstrapTable({
            url: '${basePath}/back/productMeasuringUnit/listSelectedMeasuringUnitByProductTypeId.sjson',      	/**请求后台的URL（*）**/
            method: 'post',                       	/**请求方式（*）**/
            contentType: "application/x-www-form-urlencoded",
            dataType: 'json',
            toolbar: '#toolbar3',               	    /**工具按钮用哪个容器**/
            striped: false,                      	/**是否显示行间隔色**/
            cache: false,                       	/**是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）**/
            pagination: true,                   	/**是否显示分页（*）**/
            sortable: false,                    	/**是否启用排序**/
            sortOrder: 'asc',                   	/**排序方式**/
            queryParamsType: '',
            sidePagination: 'server',           	/**分页方式：client客户端分页，server服务端分页（*）**/
            pageNumber: 1,
            pageSize:10,							/**初始化加载第一页，默认第一页**/
            pageList: [5,10, 25, 50],        	    /**可供选择的每页的行数（*）**/
            search: false,                       	/**是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大**/
            strictSearch: true,
            showColumns: true,                  	/**是否显示所有的列**/
            showRefresh: true,                  	/**是否显示刷新按钮**/
            minimumCountColumns: 2,             	/**最少允许的列数**/
            clickToSelect: true,                	/**是否启用点击选中行**/
            uniqueId: 'measuringUnitId',            /**每一行的唯一标识，一般为主键列**/
            singleSelect  : true,                   /** 单选checkbox**/
            showToggle:false,                    	/**是否显示详细视图和列表视图的切换按钮**/
            cardView: false,                    	/**是否显示详细视图**/
            detailView: false,                  	/**是否显示父子表**/
            columns: [
                {checkbox: true},
                {field: 'Number', title: '编号', align: 'center', valign: 'middle', width: '50px',
                    formatter: function (value, row, index) {
                        var page = $tt_measuringUnitList.bootstrapTable("getPage");
                        return page.pageSize * (page.pageNumber - 1) + index + 1;
                    }
                },
                {field: 'name', title: '计量单位名称', align: 'center', valign: 'middle'}
            ],
            queryParams: function (params) {
                /**特别说明，返回的参数的值为空，则当前参数不会发送到服务器端,这种指定请求参数的方式和datatables控价类似**/
                params.name = $("#q_name").val();
                params.productTypeId = $("#productTypeId").val();
                params.currentPage = params.pageNumber;
                return params;
            }
        });

        /**表单查询**/
        $("#btn_query_queryMeasuringUnit").click(function () {
            $("#tt_measuringUnitList").bootstrapTable('refresh');
        });
    }

    /**
     * 添加未关联计量单位
     */
    function addUnselectMeasuringUnitInfo(){
        var selectedRow= $("#tt").bootstrapTable('getSelections');
        if(selectedRow.length==1){
            var levelCode=selectedRow[0].levelCode;
            if(levelCode==3){
                var selectedRow= $("#tt").bootstrapTable('getSelections');
                $("#form_assignMeasuringUnit").find("#productTypeId").val(selectedRow[0].productTypeId);/**获取到productTypeId值并放入隐藏域**/
                $("#btn_add_queryMeasuringUnit").attr("data-target", "#queryUnselectMeasuringUnitModal");/**当前分类级别是三级分类才可打开对应的模态框**/
                $("#queryUnselectMeasuringUnitModalLabel").empty().text("未关联计量单位");
                /** 重置当前未关联的列表中的查询条件**/
                $("#qform_queryUnselectMeasuringUnit")[0].reset();
                /** 刷新当前未关联的列表**/
                $("#tt_unselectMeasuringUnitList").bootstrapTable('refresh');
                showUnselectMeasuringUnitList();
            }else{
                $("#btn_add_queryMeasuringUnit").attr("data-target",""); /**没有选中记录不可打开模态框**/
                showMsg("当前分类级别不是三级分类，不可查询计量单位！");
            }
        }else{
            $("#btn_add_queryMeasuringUnit").attr("data-target",""); /**没有选中记录不可打开模态框**/
            showMsg("请选择一行记录！");
        }
    }

    /**
     * 添加未关联的计量单位列表
     */
    function showUnselectMeasuringUnitList(){
        var $tt_unselectMeasuringUnitList = $("#tt_unselectMeasuringUnitList").bootstrapTable({
            url: '${basePath}/back/productMeasuringUnit/listUnselectedProductMeasuringUnitByProductTypeId.sjson',         	/**请求后台的URL（*）**/
            method: 'post',                       	/**请求方式（*）**/
            contentType: "application/x-www-form-urlencoded",
            dataType: 'json',
            //toolbar: '#toolbar',               	    /**工具按钮用哪个容器**/
            striped: true,                      	/**是否显示行间隔色**/
            cache: false,                       	/**是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）**/
            pagination: true,                   	/**是否显示分页（*）**/
            sortable: false,                    	/**是否启用排序**/
            sortOrder: 'asc',                   	/**排序方式**/
            queryParamsType: '',
            sidePagination: 'server',           	/**分页方式：client客户端分页，server服务端分页（*）**/
            pageNumber: 1,
            pageSize:10,							/**初始化加载第一页，默认第一页**/
            pageList: [5,10, 25, 50],        	    /**可供选择的每页的行数（*）**/
            search: false,                       	/**是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大**/
            strictSearch: true,
            showColumns: false,                  	/**是否显示所有的列**/
            showRefresh: false,                  	/**是否显示刷新按钮**/
            minimumCountColumns: 2,             	/**最少允许的列数**/
            clickToSelect: true,                	/**是否启用点击选中行**/
            uniqueId: 'measuringUnitId',            /**每一行的唯一标识，一般为主键列**/
            singleSelect  : false,                 /**单选checkbox**/
            showToggle:false,                    	/**是否显示详细视图和列表视图的切换按钮**/
            cardView: false,                    	/**是否显示详细视图**/
            detailView: false,                  	/**是否显示父子表**/
            columns: [
                {checkbox: true},
                {field: 'Number', title: '编号', align: 'center', valign: 'middle', width: '50px',
                    formatter: function (value, row, index) {
                        var page = $tt_unselectMeasuringUnitList.bootstrapTable("getPage");
                        return page.pageSize * (page.pageNumber - 1) + index + 1;
                    }
                },
                {field: 'name', title: '计量单位名称', align: 'center', valign: 'middle'}
            ],
            queryParams: function (params) {
                /**特别说明，返回的参数的值为空，则当前参数不会发送到服务器端,这种指定请求参数的方式和datatables控价类似**/
                params.name = $("#q_sub_name").val();
                params.productTypeId = $("#productTypeId").val();
                params.currentPage = params.pageNumber;
                return params;
            }
        });

        /**表单查询**/
        $("#btn_query_queryUnselectMeasuringUnit").click(function () {
            $("#tt_unselectMeasuringUnitList").bootstrapTable('refresh');
        });
    }

    /**保存添加的未关联计量单位**/
    function saveMeasuringUnit(){
        /**获取选中行的id**/
        var selectedRow= $("#tt_unselectMeasuringUnitList").bootstrapTable('getSelections');
        var measuringUnitIds="";
        if(selectedRow.length>0){
            for(var i=0;i<selectedRow.length;i++){
                var  measuringUnitId=selectedRow[i].measuringUnitId;
                if(i==selectedRow.length-1){
                    measuringUnitIds+=measuringUnitId;
                }else{
                    measuringUnitIds+=measuringUnitId+",";
                }
            }
            /** 获取值放入隐藏域 **/
            $("#measuringUnitIds").val(measuringUnitIds);
            var options = {
                url: $("#form_assignMeasuringUnit").attr("action"),
                type : "post",
                dataType:"json",
                success : function(data) {
                    if(data.success==true){
                        /** 先重置已关联计量单位窗口查询框的值**/
                        $("#qform_queryMeasuringUnit")[0].reset();
                        /** 刷新已关联计量单位窗口的列表**/
                        $("#tt_measuringUnitList").bootstrapTable('refresh');
                        /** 隐藏未关联计量单位弹出模态框窗口**/
                        $('#queryUnselectMeasuringUnitModal').modal('hide');
                        showMsg("保存成功！");
                    }else{
                        showMsg("保存失败！");
                    }
                }
            };
            $("#form_assignMeasuringUnit").ajaxSubmit(options);
        }else{
            showMsg("请选择一行记录！");
        }
    }

    /**
     * 删除已关联的计量单位
     */
    function deleteMeasuringUnitInfo() {
        /**获取选中行的id**/
        var selectedRow = $("#tt_measuringUnitList").bootstrapTable('getSelections');
        var selectedRow_tt = $("#tt").bootstrapTable('getSelections');
        if (selectedRow.length == 1) {
            var productTypeId=selectedRow_tt[0].productTypeId;
            var measuringUnitId = selectedRow[0].measuringUnitId;
            showConfirm("你确定要删除吗?", function () {
                $.ajax({
                    type: "POST",
                    dataType: "JSON",
                    url: "${basePath}/back/productMeasuringUnit/deleteProductMeasuringUnit.sjson",
                    data: {productTypeId: productTypeId,measuringUnitId: measuringUnitId},
                    success: function (data) {
                        if (data.success == true) {
                            $("#tt_measuringUnitList").bootstrapTable('refresh');/**刷新已经选中的列表**/
                            $("#tt_unselectMeasuringUnitList").bootstrapTable('refresh');/**刷新未选中的列表**/
                        }
                    }
                });
            });
        } else {
            showMsg("请选择一行记录！");
        }
    }

</script>

<!--模态框（Modal） -->
<div class="modal fade" id="queryMeasuringUnitModal" tabindex="-1" role="dialog" aria-labelledby="queryMeasuringUnitModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog" style="width:90%">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h5 class="modal-title" id="queryMeasuringUnitModalLabel"></h5>
            </div>

            <div class="modal-body" style="padding-top: 5px;">
                <!-- 查询条件 -->
                <div class="panel panel-default" style="margin-top: 15px;">
                    <div class="panel-body">
                        <form id="qform_queryMeasuringUnit" class="form-horizontal">
                            <div class="form-group" style="margin-top:15px">
                                <div class="col-sm-3" style="width: 260px;">
                                    <div class="input-group input-group-sm">
                                        <span class="input-group-addon">计量单位名称</span>
                                        <input type="text" class="form-control" id="q_name" name="name" placeholder="请输入计量单位名称"/>
                                    </div>
                                </div>
                                <div class="col-sm-2" style="width:160px;">
                                    <button type="button" id="btn_query_queryMeasuringUnit" class="btn btn-primary">查询</button>&nbsp;&nbsp;
                                    <button type="reset" class="btn btn-success" id="resetBtn" >重置</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>

                <!-- 工具栏 -->
                <div id="toolbar3" class="btn-group">
                    <button id="btn_add_queryMeasuringUnit" type="button" class="btn btn-default" data-toggle="modal" onclick="addUnselectMeasuringUnitInfo()">
                        <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增未关联计量单位
                    </button>
                    <button id="btn_delete_queryMeasuringUnit" type="button" class="btn btn-default" onclick="deleteMeasuringUnitInfo()">
                        <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
                    </button>
                </div>

                <form id="form_measuringUnitList"  method="post" action="">
                    <!-- 渲染的table -->
                    <table id="tt_measuringUnitList"></table>
                </form>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<div class="modal fade" id="queryUnselectMeasuringUnitModal" tabindex="-1" role="dialog" aria-labelledby="queryUnselectMeasuringUnitModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog" style="width:70%">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h5 class="modal-title" id="queryUnselectMeasuringUnitModalLabel"></h5>
            </div>

            <div class="modal-body" style="padding-top: 5px;">
                <!-- 查询条件 -->
                <div class="panel panel-default" style="margin-top: 15px;">
                    <div class="panel-body">
                        <form id="qform_queryUnselectMeasuringUnit" class="form-horizontal">
                            <div class="form-group" style="margin-top:15px">
                                <div class="col-sm-3" style="width: 260px;">
                                    <div class="input-group input-group-sm">
                                        <span class="input-group-addon">计量单位名称</span>
                                        <input type="text" class="form-control" id="q_sub_name" name="name" placeholder="请输入计量单位名称"/>
                                    </div>
                                </div>
                                <div class="col-sm-2" style="width:160px;">
                                    <button type="button" id="btn_query_queryUnselectMeasuringUnit" class="btn btn-primary">查询</button>&nbsp;&nbsp;
                                    <button type="reset" class="btn btn-success" id="resetBtn2" >重置</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                <form id="form_assignMeasuringUnit"  method="post" action="${basePath}/back/productMeasuringUnit/saveSelectedProductMeasuringUnit.sjson">
                    <input id="productTypeId" type="hidden" name="productTypeId" />
                    <input id="measuringUnitIds" type="hidden" name="measuringUnitIds" />
                    <!-- 渲染的table -->
                    <table id="tt_unselectMeasuringUnitList"></table>
                </form>
            </div>

            <div class="modal-footer">
                <button type="button" id="validateBtn" class="btn btn-primary"  onclick="saveMeasuringUnit()">确认关联</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>